package com.zxit.controller;


import com.zxit.entity.Result;
import com.zxit.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {

    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

//保存到本地磁盘
//    @PostMapping(value = "/upload")
//    public Result upload(String name, Integer age, MultipartFile file) throws IOException {
//        log.info("接收参数：{}，{}，{}", name, age, file);
//        //保存文件 获取原始文件名
//        String originalFilename = file.getOriginalFilename();
//
//        //获取文件拓展名
//        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
//
//        //新的文件名
//        String newFileName= UUID.randomUUID().toString() + extension;
//
//        //存储到本地
//        file.transferTo(new File("E:/Springboot/image/" + newFileName));
//
//        return Result.success();
//    }
    @PostMapping(value = "/upload")
    public Result upload( MultipartFile file) throws Exception {
        log.info("文件上传：{}", file);
        //将文件交给OSS管理，引入工具类
        String url = aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());
        log.info("文件上传成功：{}", url);
        return Result.success(url);
    }
}
